package com.ysd.lis.controller.eq;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.eq.*;
import com.ysd.lis.service.eq.EqSpotcheckTaskEqService;
import com.ysd.lis.service.eq.EqSpotcheckTaskEqStItService;
import com.ysd.lis.service.eq.EqSpotcheckTaskEqStService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 点检记录设备的巡检标准细项 前端控制器
 * </p>
 *
 * @author WH
 * @since 2024-08-26
 */
@RestController
@RequestMapping("/api/eq/spotcheckTaskEqStIt")
public class EqSpotcheckTaskEqStItController extends BaseController {

    @Autowired
    private EqSpotcheckTaskEqStItService eqSpotcheckTaskEqStItService;

    @Autowired
    private EqSpotcheckTaskEqService eqSpotcheckTaskEqService;

    @Autowired
    private EqSpotcheckTaskEqStService eqSpotcheckTaskEqStServiceService;
    @Autowired
    private EqSpotcheckTaskEqStItService eqSpotcheckTaskEqStItServiceService;
    @ApiOperation("查询点检记录设备的点检标准细项")
    @GetMapping("/findSpotcheckTaskEqStItList")
    public Result findSpotcheckTaskEqStItList(EqSpotcheckTaskEqStIt eqSpotcheckTaskEqStIt) {
        return eqSpotcheckTaskEqStItService.findSpotcheckTaskEqStItList(eqSpotcheckTaskEqStIt);
    }

    @ApiOperation("批量修改或新增")
    @PostMapping("/saveOrUpdateTaskEqStIt")
    public Result saveOrUpdateTaskEqStIt(@RequestBody List<EqSpotcheckTaskEqStIt> eqSpotcheckTaskEqStIts) {
        //同时修改结果/不通过/未检部分
        EqSpotcheckTaskEq eqSpotcheckTaskEq = eqSpotcheckTaskEqService.getById(eqSpotcheckTaskEqStIts.get(0).getTaskEqId());
        List<String> taskEqStIds = eqSpotcheckTaskEqStIts.stream().map(EqSpotcheckTaskEqStIt::getTaskEqStId).distinct().collect(Collectors.toList());

        //获取所有的巡检标准
        List<EqSpotcheckTaskEqSt> eqSpotcheckTaskEqSts = eqSpotcheckTaskEqStServiceService.list(new LambdaQueryWrapper<EqSpotcheckTaskEqSt>().in(EqSpotcheckTaskEqSt::getId, taskEqStIds).eq(EqSpotcheckTaskEqSt::getDelFlag, 0));
        String isNormal="";
        List<String> noPass=new ArrayList<>();
        List<String> noCheck=new ArrayList<>();
        List<String> isNormales = eqSpotcheckTaskEqStIts.stream().filter(item-> StringUtils.isNotBlank(item.getIsNormal())).map(EqSpotcheckTaskEqStIt::getIsNormal).distinct().collect(Collectors.toList());

        if (CollectionUtils.isNotEmpty(isNormales)){
            isNormal="正常";
            for (String item : isNormales) {
                if ("异常".equals(item)){
                    isNormal=item;
                    break;
                }
            }
        }
        for (EqSpotcheckTaskEqStIt eqSpotcheckTaskEqStIt : eqSpotcheckTaskEqStIts) {
            if ("异常".equals(eqSpotcheckTaskEqStIt.getIsNormal())){
                EqSpotcheckTaskEqSt eqPatrolTaskEqSt = eqSpotcheckTaskEqSts.stream().filter(item -> item.getId().equals(eqSpotcheckTaskEqStIt.getTaskEqStId())).findFirst().get();
                noPass.add(eqPatrolTaskEqSt.getStandardName());
            }
        }
        for (EqSpotcheckTaskEqStIt eqSpotcheckTaskEqStIt : eqSpotcheckTaskEqStIts) {
            if (StringUtils.isBlank(eqSpotcheckTaskEqStIt.getCheckResult())){
                EqSpotcheckTaskEqSt eqPatrolTaskEqSt = eqSpotcheckTaskEqSts.stream().filter(item -> item.getId().equals(eqSpotcheckTaskEqStIt.getTaskEqStId())).findFirst().get();
                noCheck.add(eqPatrolTaskEqSt.getStandardName());
            }
        }
        List<String> noPassEs = noPass.stream().distinct().collect(Collectors.toList());
        eqSpotcheckTaskEq.setCheckResult(isNormal);
        eqSpotcheckTaskEq.setNoPass(String.join(",", noPassEs));
        eqSpotcheckTaskEq.setNoCheck(String.join(",", noCheck));
        eqSpotcheckTaskEqService.updateById(eqSpotcheckTaskEq);
        eqSpotcheckTaskEqStItServiceService.saveOrUpdateBatch(eqSpotcheckTaskEqStIts);
        return Result.succ(1, "保存成功！", null);
    }

}
